﻿Public Class perTipoPA
    Inherits PersistenciaPadrao.AcessoBD

    ReadOnly Property sqlConsulta() As String

        Get
            Dim sSql As String
            sSql = "SELECT codTipoPA as 'Código', DescTipoPA as 'Tipo Ponto de Atendimento'"
            sSql &= " FROM com_TipoPA"
            sSql &= " ORDER BY descTipoPA"

            Return sSql
        End Get

    End Property

    Public Function inserirTIpoPA(ByVal sDescTipoPA As String, ByVal bEspComplemento As Boolean) As Int32
        Dim sSql As String

        sSql = "INSERT INTO com_tipoPA" & Environment.NewLine &
               "           (descTipoPA" & Environment.NewLine &
               "            ,espComplemento" & Environment.NewLine &
               "            )" & Environment.NewLine &
               "     VALUES" & Environment.NewLine &
               "           (" & Environment.NewLine &
               "            @descTipoPA" & Environment.NewLine &
               "           ,@espComplemento" & Environment.NewLine &
               "            ); SELECT LAST_INSERT_ID()"

        MyBase.limparParametros()
        MyBase.inserirParametro("@descTipoPA", sDescTipoPA)
        MyBase.inserirParametro("@espComplemento", bEspComplemento)

        Return MyBase.executarConsultaCampo(sSql, True)

    End Function

    Public Sub editarTipoPA(ByVal iCodTipoPA As Int32,
                           ByVal sDescricao As String,
                           ByVal bEspComplemento As Boolean)
        Dim sSql As String

        sSql = "UPDATE com_TipoPA SET" & Environment.NewLine &
               "           descTIpoPA = @descricao" & Environment.NewLine &
               "           ,espComplemento=@espComplemento" & Environment.NewLine &
               "WHERE codTipoPA=@codTipoPA"

        MyBase.limparParametros()
        MyBase.inserirParametro("@descricao", sDescricao)
        MyBase.inserirParametro("@espComplemento", bEspComplemento)
        MyBase.inserirParametro("@codTipoPA", iCodTipoPA)

        MyBase.executarAcao(sSql, True)

    End Sub

    Public Function verificarTipoPaCadastrado(ByVal iCodTipoPA As Int32,
                                              ByVal sDescricao As String) As Boolean
        Dim sSql As String

        sSql = "SELECT codTipoPA" & Environment.NewLine &
               " FROM com_TipoPA" & Environment.NewLine &
               "WHERE descTipoPA = @descricao " & Environment.NewLine
        If iCodTipoPA > 0 Then
            sSql &= "  AND codTipoPA<>@codTipoPA"
        End If

        MyBase.limparParametros()
        MyBase.inserirParametro("@descricao", sDescricao)
        If iCodTipoPA > 0 Then
            MyBase.inserirParametro("@codTipoPA", iCodTipoPA)
        End If

        Return MyBase.executarConsulta(sSql, True).Rows.Count = 0

    End Function

    Public Function selecionarDadosTiposPA(ByVal iCodTipoPA As Int32) As DataRow
        Dim sSql As String
        Dim dtbTipoPA As DataTable

        sSql = "SELECT *" & Environment.NewLine &
                "  FROM com_TipoPA" & Environment.NewLine &
                " WHERE codTIpoPA=@codTipoPA"

        MyBase.limparParametros()
        MyBase.inserirParametro("@codTIpoPA", iCodTipoPA)

        dtbTipoPA = MyBase.executarConsulta(sSql, True)

        If dtbTipoPA.Rows.Count > 0 Then
            Return dtbTipoPA.Rows(0)
        Else
            Return Nothing
        End If

    End Function

    Public Sub excluirTipoPA(ByVal iCodTipoPA As Int32)
        Dim sSql As String

        sSql = "DELETE FROM com_TipoPA" & Environment.NewLine &
               " WHERE codTipoPA = @codTipoPA;"

        MyBase.limparParametros()
        MyBase.inserirParametro("@codTipoPA", iCodTipoPA)

        MyBase.executarAcao(sSql, True)

    End Sub


End Class
